<?php 	
	session_start();
	

	require_once('libs/constants.php');
	require('libs/classes/userClass.php');
	require_once('libs/functions.php');
	$page = $_GET['page'];		
		$userObj = new userClass();
	// 	if(isset($_POST['shop_name'])){
		
	// 		$params = array($_POST['email']);
	// 		$result = $userObj->fetch_one("SELECT id FROM user WHERE email=?",$params);

	// 				// echo "<pre>";
	// 				// print_r($result);
	// 				// echo "</pre>";

	// 			$_POST['shop_name'] = str_replace(" ", '_', $_POST['shop_name']);
	// 			$_POST['shop_name'] = str_replace("-", '_', $_POST['shop_name']);
	// 			$con=mysql_connect("localhost",'neoflux',',?bUuk.h{!9V');		
	// 			$params['db_name'] = 'neoflux_'.$_POST['shop_name'];
	// 			$aaa= mysql_query("CREATE DATABASE {$params['db_name']}",$con);
				
	// 			if($result && count($result)>0){
	// 						// echo "<pre>";
	// 						// print_r($result);
	// 						// echo "</pre>";
	// 		 		$last_id=$result['id'];
	// 		 		$_SESSION['session_user'] = $last_id;
	// 			}else{
	// 				$params=array();
	// 				$params['email']=$_POST['email'];
	// 				$params['password']=md5($_POST['password']);
	// 				$params['language_id']=2;		
	// 				$params['created']= date("Y-m-d H:i:s");
	// 				$last_id= $userObj->insert('user',$params);
	// 				$_SESSION['session_user'] = $last_id;
	// 			}
	// 			$params=array();
	// 			$params['user_id']=$last_id;
	// 			$params['name']=$_POST['shop_name'];
	// 			$params['logo']='';
	// 			$params['description']='';
	// 			$params['db_name']=$_POST['shop_name'];
	// 			$params['db_user']='neoflux';
	// 			$params['db_pass']=',?bUuk.h{!9V';
	// 			$params['active']=1;

	// 			$last_id= $userObj->insert('user_shops',$params);
	// 			$sql = file_get_contents("neoflux_shop.sql");


	// 			$params=array();
	// 			$params['hostname']=DB_SERVER;
	// 			$params['dbname']='neoflux_'.$_POST['shop_name'];
	// 			$params['username']='neoflux';
	// 			$params['password']=',?bUuk.h{!9V';
	// 			$shopObj = new mysqlClass($params);
	// #conect to shop database	

	// 			$shopObj->execute_query($sql);
	// 			$_SESSION['shop_creation'] =$last_id;
	// 			$_SESSION['shop_name'] ='neoflux_'.$_POST['shop_name'];

	// 	}else if($_GET['page']=='generate-shop'){
	// 		header( 'Location: http://www.neo-flux.com/admin/profile.html' ) ;
	// 	}
			
	
	
	//create mysql user account 
	

	$user = $userObj->get_user_by_session($_SESSION['session_user']);
	$user = $user[0];
			
	if(count($user)==0){
		if($_POST['password']){
			$rows= $userObj->log_in($_POST['email'],$_POST['password']);
			$_SESSION['session_user'] = $rows[0]['session_user'];
			$user = $userObj->get_user_by_session($_SESSION['session_user']);
			$user=$user[0];
		}else{
			header("location:login.php");
		}
	}
					
	// $dictionary_app = get_dictionary($user['language_id']);
			
	
	if(isset($_SESSION['shop_creation'])){
		$params=array($_SESSION['shop_creation']);
		$row = $userObj->fetch_result("SELECT * FROM user_shops WHERE id=?",$params);
		$shop = $row[0];
		$_SESSION['shop_name']= 'neoflux_'.$shop['db_name'];
			 // print_r($shop);
	}else{
		$params=array($user['id']);
		$row = $userObj->fetch_result("SELECT * FROM user_shops WHERE user_id=?",$params);
		$shop = $row[0];
		$_SESSION['shop_name']= 'neoflux_'.$shop['db_name'];
		$_SESSION['shop_creation']=$shop['id'];
	}
	if($shop){
		// $params['hostname']=DB_SERVER;
		// $params['dbname']='neoflux_'.$shop['db_name'];
		// $params['username']=$shop['db_user'];
		// $params['password']=$shop['db_pass'];
	#conect to shop database	
		$shopObj = new mysqlClass();
		$language = $shopObj->fetch_result("SELECT id FROM languages WHERE is_default=1");
		$available_lang= $shopObj->fetch_result("SELECT * FROM languages WHERE active=1");
		$dictionary = get_dictionary_shop(2);
		$available_lang[0]['id'] =2;
		$language[0]['id']=2;
		 
	}	
		
			
	include_once("global_includes/head.php");
	


	?>

	<body>
		<?php 
					
		?>
		<div id='spiner_wrapper' style='display:none;width:100%;height:100%;position:absolute;z-index:200000000;background:rgba(0,0,0,0.5);'>
		</div>
		<?php include_once("global_includes/navbar.php"); ?>

		<div class="main-container container-fluid" id='main-container'>
			<a class="menu-toggler" id="menu-toggler" href="#">
				<span class="menu-text"></span>
			</a>

			<?php 
				if($page!='generate-shop')
				include_once('global_includes/sidebar.php'); 
			?>

			<div class="main-content" <?php if($page=='generate-shop')  echo " style='margin-left:0' "; ?>>
				<?php include_once("global_includes/breadcrumb.php"); ?>

				<div class="page-content">
					<div class="row-fluid">
						<div class="span12">
							<!--PAGE CONTENT BEGINS-->
							
							<?php
							
								 include_once("global_includes/pages/".$page.".php");
								 
							?>
						
							<!--PAGE CONTENT ENDS-->
						</div><!--/.span-->
					</div><!--/.row-fluid-->
				</div><!--/.page-content-->

				<?php
				 	if($_GET['page']=='site_pages'){
				 		include_once("global_includes/ace_settings.php"); 
				 	}
				 	
				 ?>

			</div><!--/.main-content-->

		</div><!--/.main-container-->
		<input type ='hidden' value='<?php echo SERVER_PATH ?>' id='server_path'>
		<input type ='hidden' value='<?php echo "http://www.".$shop['db_name'].".neo-flux.com" ?>' id='server_path_shop'>
		<?php include_once('global_includes/footer.php'); 
		
		?>
<!-- js pentru profil -->
<!-- <script src="js/profile.js"></script> -->

<!-- scripturi generale -->
<script>
	var SERVER_PATH = "<?php echo SERVER_PATH ?>";
</script>
<script src="<?php echo SERVER_PATH ?>js/js_utils.js"></script>

<!-- js pentru wizzard eefectiv -->
<?php if($_GET['page']=='generate-shop'){ ?>
<script src="<?php echo SERVER_PATH ?>assets/owl/owl.carousel.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="<?php echo SERVER_PATH ?>js/wizard.js"></script>

<?php 
}
if($_GET['page']=='site_pages'){ ?>
<!-- js for site pages  -->
<script src="<?php echo SERVER_PATH ?>js/uploadify/jquery.uploadify.min.js"></script>
<script src="<?php echo SERVER_PATH ?>js/site_pages.js"></script>

<?php } if($_GET['page']=='products'){ ?>
<script src="<?php echo SERVER_PATH ?>js/uploadify/jquery.uploadify.min.js"></script>
<script src="<?php echo SERVER_PATH ?>js/products.js"></script>
<?php 
}else if($_GET['page']=='companies'){ ?>
<script src="<?php echo SERVER_PATH ?>js/companies.js"></script>
<?php 
}if($_GET['page']=='company'){ ?>
<script src="<?php echo SERVER_PATH ?>js/company.js"></script>
<?php 
}else if($_GET['page']=='product'){?>
<script src="<?php echo SERVER_PATH ?>js/product.js"></script>
<?php 
}else if($_GET['page']=='clients'){?>
<script src="<?php echo SERVER_PATH ?>js/clients.js"></script>
<?php 
}else if($_GET['page']=='wishlist'){?>
<script src="<?php echo SERVER_PATH ?>js/wishlist.js"></script>
<?php 
}else if($_GET['page']=='client_wishlist'){?>
<script src="<?php echo SERVER_PATH ?>js/client_wishlist.js"></script>
<?php 
}else if($_GET['page']=='orders'){?>
<script src="<?php echo SERVER_PATH ?>js/orders.js"></script>
<?php 
}else if($_GET['page']=='reports'){?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="<?php echo SERVER_PATH ?>js/reports.js"></script>
<?php 
}else if($_GET['page']=='settings'){?>

<script src="<?php echo SERVER_PATH ?>js/settings.js"></script>
<?php 
}else if($_GET['page']=='profile'){?>

<script src="<?php echo SERVER_PATH ?>js/profile.js"></script>
<?php 
}

?>
<script type="text/javascript">
	// CHECK STOCK

	$(document).ready(function(){
		checkStock();
	});

	checkStock=function(){

		var obj={};
		obj.action="checkStock";

//		$.ajax({
//	        url: "http://www.neo-flux.com/wizard/libs/checkStock.php",
//	        type: "POST",
//	        data: obj,
//	       
//	        success: function(data){
//	        	var x=JSON.parse(data);
//	        	if(x.length>0){
//	        	// console.log(x[0][0]);
//	        	addGritter(x);
//	        }
//	        	// console.log(data);
//	        },
//	        error:function(){
//	            // alert("Error");
//	        }
//	    });

	}

addGritter=function(data){
	var title="This is a warning notification";
	var text="Actual stock is under limit for :<br/>";
	// console.log(data.length);
	for(var i=0;i<data.length;i++){  //BLOCAT LA 1 !!!
		var linie=(i+1)+") "+data[i][2]+" , in stock: "+data[i][1]+"<br/>";
		text+=linie;
	}

	$.gritter.add({
		title: title,
		text: text,
		class_name:'gritter-error'
	});
	return false; 	

}

	// END CHECK STOCK





</script>
<script>
function change_shop(x){
	dataString = 'change_shop='+x
	$.ajax({
	        url: "http://www.neo-flux.com/wizard/libs/change_shop.php",
	        type: "POST",
	        data: dataString,
	        success: function(data){
	        	location.reload(); 
	        }
	    });
}
</script>